package cn.meng.base.utils;

import cn.hutool.core.date.DateTime;
import cn.meng.base.handler.EmployeeExcelImportListener;
import cn.meng.base.service.EmployeeService;
import cn.meng.base.vo.BusinessException;
import cn.meng.base.vo.EmployeeVo;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 员工信息（Employee）Excel导入工具类
 * @author 万梦辉
 */
@Component
public class ExcelUtil {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private final String EMPLOYEEVO = "EmployeeVo";

    @Autowired
    EmployeeService employeeService;

    private ExcelReaderBuilder read;

    /**
     * Excel导入
     * @param file  前端直接通过组件上传文件即可，后端接收的格式为 MultipartFile
     * @param c   Excel对应的VO的class
     * @throws BusinessException
     * 注意：如果有新的VO需要支持导入功能、直接再if后面加上else if 增加传入对应的vo的class判断，不可直接写为else，必须写为else if
     */
    public void importData(MultipartFile file,Class c) throws BusinessException {
        try {
            if(EMPLOYEEVO.equals(c.getSimpleName())) {
                read = EasyExcel.read(file.getInputStream(), EmployeeVo.class, new EmployeeExcelImportListener(employeeService));
            }
        } catch (IOException e) {
            log.error("导入数据失败");
            throw new BusinessException(500,"导入数据失败");
        }
        read.sheet().doRead();
        log.info("导入成功");
    }

    /**
     * Excel导出，采用泛型适配多个不同的VO
     * @param response
     * @param c
     * @param data
     * @param <T>
     * @throws BusinessException
     */
    public <T> void exportData(HttpServletResponse response, Class<T> c, List<T> data) throws BusinessException {
        String filename = new DateTime().toString() + ".xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + filename);
        try {
            EasyExcel.write(response.getOutputStream(), c).sheet();
            log.info("数据导出成功");
        } catch (IOException e) {
            log.error("数据导出失败");
            throw new BusinessException(500,"数据导出错误");
        }
    }

    /**
     * 导出模板
     * @param c 对应的VO类
     * @param <T>
     */
    public <T> void exportTemplet(HttpServletResponse response,Class<T> c) throws BusinessException {
        String filename = new DateTime().toString() + ".xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + filename);
        try {
            EasyExcel.write(response.getOutputStream(),c).sheet("模板").doWrite(new ArrayList());
            log.info("模板导出成功");
        } catch (IOException e) {
            log.error("模板导出失败");
            throw new BusinessException(500,"数据导出错误");
        }
    }

}
